package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyRegularDebtViewPagerAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.fragment.MyRegularFragment;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.SystemUtils;

/**
 * 我的定期
 */
public class MyRegularDebtActivity extends BaseActivity {

	private Context mContext = this;
	private View back;
	private View history_tv;
	private View select_chiyouzhong;
	public TextView tv_select_chiyouzhong;
	private View line_select_chiyouzhong;
	private View select_zhuanrangzhong;
	public TextView tv_select_zhuanrangzhong;
	private View line_select_zhuanrangzhong;
	private View select_yizhuanrang;
	public TextView tv_select_yizhuanrang;
	private View line_select_yizhuanrang;
	private ViewPager viewpager;
	public TextView total_tv;
	public TextView frozen_tv;
	private View total_popup;
	private View frozen_popup;
	private PopupWindow tip_popupWindow;
	private List<Fragment> list_fragment = new ArrayList<Fragment>();
	private MyRegularDebtViewPagerAdapter pagerAdapter;
	public static MyRegularDebtActivity mActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_regular_debt);
		mActivity = this;
		initView();
		initListener();
		if (getIntent().getBooleanExtra("setItem", false)) {
			int curItem = getIntent().getIntExtra("curItem", 0);
			viewpager.setCurrentItem(curItem > 2 ? 0 : curItem);
		}
	}

	@SuppressWarnings("deprecation")
	private void initView() {
		back = findViewById(R.id.condition_img);
		history_tv = findViewById(R.id.history_tv);
		total_tv = (TextView) findViewById(R.id.total_tv);
		frozen_tv = (TextView) findViewById(R.id.frozen_tv);
		total_popup = findViewById(R.id.total_popup);
		frozen_popup = findViewById(R.id.frozen_popup);
		// 持有中
		select_chiyouzhong = findViewById(R.id.select_chiyouzhong);
		tv_select_chiyouzhong = (TextView) findViewById(R.id.tv_select_chiyouzhong);
		line_select_chiyouzhong = findViewById(R.id.line_select_chiyouzhong);
		// 转让中
		select_zhuanrangzhong = findViewById(R.id.select_zhuanrangzhong);
		tv_select_zhuanrangzhong = (TextView) findViewById(R.id.tv_select_zhuanrangzhong);
		line_select_zhuanrangzhong = findViewById(R.id.line_select_zhuanrangzhong);
		// 已转让
		select_yizhuanrang = findViewById(R.id.select_yizhuanrang);
		tv_select_yizhuanrang = (TextView) findViewById(R.id.tv_select_yizhuanrang);
		line_select_yizhuanrang = findViewById(R.id.line_select_yizhuanrang);
		// viewpager
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		viewpager.setOffscreenPageLimit(2);
		list_fragment.add(MyRegularFragment.getInstance(1));// 持有中
		list_fragment.add(MyRegularFragment.getInstance(2));// 转让中
		list_fragment.add(MyRegularFragment.getInstance(3));// 已转让
		pagerAdapter = new MyRegularDebtViewPagerAdapter(
				getSupportFragmentManager(), list_fragment);
		viewpager.setAdapter(pagerAdapter);
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				changeColorState(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	private void initListener() {
		back.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				finish();
			}
		});
		history_tv.setOnClickListener(new OnSingleClickListener() {// 　历史

					@Override
					public void onSingleClick(View v) {
						SystemUtils.onEvent("Account_Regular_History", 1);
						Intent intent = new Intent(mContext,
								HistoryActivity.class);
						startActivity(intent);
					}
				});
		total_popup.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				showTipPopupwindow(v, "您定期投资成功的金额(不含快快享福),包括冻结资产");
			}
		});
		frozen_popup.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				showTipPopupwindow(v, "您定期投资未到账的预期收益(不包含快快享福预期收益)");
			}
		});
		select_chiyouzhong.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				viewpager.setCurrentItem(0);
			}
		});
		select_zhuanrangzhong.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				viewpager.setCurrentItem(1);
			}
		});
		select_yizhuanrang.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				viewpager.setCurrentItem(2);
			}
		});
	}

	private void changeColorState(int position) {
		switch (position) {

		case 0:
			tv_select_chiyouzhong.setTextColor(Color.parseColor("#635FBD"));
			line_select_chiyouzhong.setBackgroundColor(Color
					.parseColor("#635FBD"));
			tv_select_zhuanrangzhong.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_zhuanrangzhong.setBackgroundColor(Color
					.parseColor("#00000000"));
			tv_select_yizhuanrang.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_yizhuanrang.setBackgroundColor(Color
					.parseColor("#00000000"));
			break;

		case 1:
			tv_select_chiyouzhong.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_chiyouzhong.setBackgroundColor(Color
					.parseColor("#00000000"));
			tv_select_zhuanrangzhong.setTextColor(Color.parseColor("#635FBD"));
			line_select_zhuanrangzhong.setBackgroundColor(Color
					.parseColor("#635FBD"));
			tv_select_yizhuanrang.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_yizhuanrang.setBackgroundColor(Color
					.parseColor("#00000000"));
			break;

		case 2:
			tv_select_chiyouzhong.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_chiyouzhong.setBackgroundColor(Color
					.parseColor("#00000000"));
			tv_select_zhuanrangzhong.setTextColor(Color.parseColor("#B2B2B2"));
			line_select_zhuanrangzhong.setBackgroundColor(Color
					.parseColor("#00000000"));
			tv_select_yizhuanrang.setTextColor(Color.parseColor("#635FBD"));
			line_select_yizhuanrang.setBackgroundColor(Color
					.parseColor("#635FBD"));
			break;

		default:
			break;
		}
	}

	public List<Fragment> getListFragment() {
		return list_fragment;
	}

	/**
	 * 弹出气泡提示popupwindow
	 * 
	 * @param targetView
	 * @param desc
	 */
	private void showTipPopupwindow(View targetView, String desc) {
		if (tip_popupWindow != null) {
			tip_popupWindow.dismiss();
			tip_popupWindow = null;
		}
		tip_popupWindow = DialogUtils.showTipPopupWindow(mContext, targetView,
				desc);
	}
}
